// pages/contract-input/index.js
Page({
  data: {
    contractId: ''
  },

  onLoad() {
    console.log('输入合同ID页面加载完成');
  },

  // 合同ID输入
  onContractIdInput(e) {
    this.setData({
      contractId: e.detail.value.trim()
    });
  },

  // 确认并加载合同
  async confirmAndLoad() {
    const { contractId } = this.data;

    if (!contractId) {
      wx.showToast({
        title: '请输入合同ID',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    try {
      const res = await wx.cloud.callFunction({
        name: 'contract',
        data: {
          apiType: 'getContractInfo',
          contractId: contractId
        }
      });

      wx.hideLoading();

      if (res.result.res.code === 200) {
        const contractData = res.result.res.data;
        
        // 跳转到签名页面，传递合同ID
        wx.navigateTo({
          url: `/pages/signature/index?contractId=${contractData._id}&flow=tenant`
        });
      } else {
        wx.showToast({
          title: res.result.res.msg || '合同不存在',
          icon: 'none',
          duration: 2000
        });
      }
    } catch (error) {
      console.error('加载合同失败:', error);
      wx.hideLoading();
      wx.showToast({
        title: '加载失败，请重试',
        icon: 'none',
        duration: 2000
      });
    }
  }
});

